package io.github.stary1993.kite.admin.controller;

import io.github.stary1993.kite.core.util.R;
import io.github.stary1993.kite.core.util.RedisUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "Redis模块")
@RequiredArgsConstructor
@RestController
@RequestMapping("/redis")
public class RedisController {


    @PostMapping
    @Operation(summary = "设置Redis键值对")
    @Parameters(value = {
            @Parameter(name = "key", description = "键", required = true),
            @Parameter(name = "value", description = "值", required = true)
    })
    public R set(@RequestParam String key, @RequestParam String value) {
        return R.ok(RedisUtils.set(key, value));
    }

    @PostMapping("/map")
    @Operation(summary = "设置Redis键值对(Map)")
    @Parameters(value = {
            @Parameter(name = "key", description = "键", required = true),
            @Parameter(name = "values", description = "值")
    })
    public R set(@RequestParam String key, @RequestBody Map<String, Object> values) {
        return R.ok(RedisUtils.set(key, values));
    }


    @GetMapping
    @Operation(summary = "获取Redis值")
    @Parameter(name = "key", description = "键", required = true)
    public R get(@RequestParam String key) {
        return R.ok(RedisUtils.get(key));
    }

    @DeleteMapping
    @Operation(summary = "删除Redis值")
    @Parameter(name = "key", description = "键", required = true)
    public R delete(@RequestParam String key) {
        RedisUtils.del(key);
        return R.ok();
    }

}
